#
# Copyright (C) 2023
#
# Brick Yang <printfxxx@163.com>
#
# This program is free software. You can redistribute it and/or
# modify it as you like.
#

project('brick_utils', 'C',
	version: '1.0',
	meson_version: '>= 0.58.0',
	default_options: ['buildtype=release'])

kmake = find_program('kmake', dirs: meson.project_source_root())

cfgs = configuration_data()
cfgs.set('ppc', host_machine.cpu_family() == 'ppc' or
		host_machine.cpu_family() == 'ppc64')
cfgs.set('arm', host_machine.cpu_family() == 'arm')
cfgs.set('arm64', host_machine.cpu_family() == 'aarch64')

cfg_opts = {
	# Names			Properties			Keys
	'debug'			: ['debug',			'CONFIG_DEBUG'],
	'mtrace'		: ['mtrace',			'CONFIG_MTRACE'],
	'qoriq'			: ['qoriq',			'CONFIG_QORIQ'],
	'layerscape'		: ['layerscape',		'CONFIG_LAYERSCAPE'],
	'mtool'			: ['mtool',			'CONFIG_MTOOL'],
	'mdio_tool'		: ['mdio_tool',			'CONFIG_MDIO_TOOL'],
	'mdio_tool.kmod'	: ['mdio_tool.kmod',		'CONFIG_MDIO_TOOL_KMOD'],
	'simplebits'		: ['simplebits',		'CONFIG_SIMPLEBITS'],
	'sb.core'		: ['simplebits.core',		'CONFIG_SB_CORE'],
	'sb.platform_time'	: ['simplebits.platform_time',	'CONFIG_SB_PLATFORM_TIME'],
	'sb.driver'		: ['simplebits.driver',		'CONFIG_SB_DRIVER'],
	'sb.dpaa'		: ['simplebits.dpaa',		'CONFIG_SB_DPAA'],
	'sb.dpaa2'		: ['simplebits.dpaa2',		'CONFIG_SB_DPAA2'],
	'sb.cli'		: ['simplebits.cli',		'CONFIG_SB_CLI'],
	'pmu'			: ['pmu',			'CONFIG_PMU'],
	'pmu.kmod'		: ['pmu.kmod',			'CONFIG_PMU_KMOD'],
	'pmu.demo'		: ['pmu.demo',			'CONFIG_PMU_DEMO']
}

cfg_deps = {
	# Names			Depends	(AND)		Depends (OR)
	'qoriq'			: [['ppc'],		[]],
	'layerscape'		: [['arm64'],		[]],
	'mdio_tool.kmod'	: [['mdio_tool'],	[]],
	'sb.core'		: [['simplebits'],	[]],
	'sb.platform_time'	: [['sb.core'],		['qoriq', 'layerscape']],
	'sb.driver'		: [['sb.core'],		[]],
	'sb.dpaa'		: [['sb.driver'],	['qoriq', 'layerscape']],
	'sb.dpaa2'		: [['sb.driver'],	['layerscape']],
	'sb.cli'		: [['simplebits'],	[]],
	'pmu'			: [[],			['arm64']],
	'pmu.kmod'		: [['pmu'],		[]],
	'pmu.demo'		: [['pmu'],		[]]
}

mcfg = get_option('mcfg')
if mcfg == ''
	foreach i, x : cfg_opts
		cfgs.set(i, meson.get_external_property(x[0], false))
	endforeach
else
	keys = import('keyval').load(mcfg)
	foreach i, x : cfg_opts
		cfgs.set(i, keys.get(x[1], 'n') == 'y')
	endforeach
endif

foreach i, x : cfg_deps
	v = true
	if x[0].length() > 0
		foreach j : x[0]
			v = v and cfgs.get(j)
		endforeach
	endif
	if v == true and x[1].length() > 0
		v = false
		foreach j : x[1]
			v = v or cfgs.get(j)
		endforeach
	endif
	if v == false
		cfgs.set(i, false)
	endif
endforeach

cfgs_h = configure_file(configuration: cfgs, output: 'configs.h')

kdir = meson.get_external_property('kdir', '')
kcross = meson.get_external_property('kcross', '')
if meson.is_cross_build()
	if kcross == ''
		kcross = ''.join(meson.get_compiler('c').cmd_array(), '@@').replace('-gcc@@', '-')
	endif
elif kdir == ''
	kdir = '/lib/modules/' + run_command('uname', '-r', check: true).stdout().strip() + '/build'
endif

cflags = ['-Wundef', '-Wstrict-prototypes', '-Wno-trigraphs', '-Werror-implicit-function-declaration', '-Wno-format-security',
	  '-Wno-sign-compare', '-Wno-unused-but-set-variable', '-Wdeclaration-after-statement', '-Wno-pointer-sign']
ncflags = cflags

kenvs = environment()
menvs = environment()
if cfgs.get('debug') == true
	cflags += ['-DDEBUG']
	kenvs.append('KCFLAGS', '-DDEBUG',
		     separator: ' ')
endif
if cfgs.get('ppc') == true
	kenvs.set('ARCH', 'powerpc')
	menvs.set('ARCH', 'powerpc')
elif cfgs.get('arm') == true
	kenvs.set('ARCH', 'arm')
	menvs.set('ARCH', 'arm')
elif cfgs.get('arm64') == true
	kenvs.set('ARCH', 'arm64')
	menvs.set('ARCH', 'arm64')
else
	menvs.set('ARCH', '')
endif
if kcross != ''
	kenvs.set('CROSS_COMPILE', kcross)
endif

kbuild_cmd = [kmake, '@BUILD_ROOT@/@OUTDIR@', '@CURRENT_SOURCE_DIR@', '@INPUT@', '--', '-C', kdir,
	      'M=' + meson.project_build_root() + '/@OUTDIR@', 'modules']
kinst_cmd = 'make -C ' + kdir + ' M=@0@ INSTALL_MOD_PATH=${DESTDIR} modules_install'

add_project_arguments(cflags,  language: 'C')
add_project_arguments(ncflags, language: 'C', native: true)

subdir('kconfig')
subdir('mtrace')
if cfgs.get('mtool') == true
	subdir('mtool')
endif
if cfgs.get('mdio_tool') == true
	subdir('mdio_tool')
endif
if cfgs.get('simplebits') == true
	subdir('simplebits')
endif
if cfgs.get('pmu') == true
	subdir('pmu')
endif
